package TokenRecognizer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String args[]) throws IOException{
        AutomataMaker automataMaker = null;
        InfixToPrefix converter = new InfixToPrefix();
        AFDSimulator afdSimulator = new AFDSimulator();
        AFNSimulator afnSimulator = new AFNSimulator();
        Hashtable<String,Automata> automatas = new Hashtable();
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);
        boolean exit = false;
        Integer option;
        String name;
        String regularExpresion;
        String automataName;

        String menu = "-----------------Menu--------------\n"+
                        "0: Ingresar Expresion Regular\n"+
                        "1: Generar AFN (con Thombson)\n"+
                        "2: Generar AFD (desde AFN)\n"+
                        "3: Generar AFD (desde Expresión Regular)\n"+
                        "4: Evaluar Expresion\n"+
                        "6: Salir";

        do{
            System.out.println(menu);
            option = Integer.parseInt(reader.readLine());
            switch(option){
                case 0:{
                    System.out.println("Ingrese la expresion regular");
                    regularExpresion = reader.readLine();
                    regularExpresion = converter.getPrefix(regularExpresion);
                    automataMaker = new AutomataMaker(regularExpresion);
                }break;
                case 1:{
                    System.out.println("Ingrese id para el automata");
                    name = reader.readLine();
                    Automata tautomata = automataMaker.getThombson();
                    automatas.put(name, tautomata);
                    automatas.get(name).toFile(name);
                }break;
                case 2:{
                    System.out.println("Ingrese id para automata");
                    name = reader.readLine();
                    System.out.println("Ingrese el id del afn a transformar");
                    automataName = reader.readLine();
                    Automata tautomata = automataMaker.evalSubset(automatas.get(automataName));
                    automatas.put(name, tautomata);
                    automatas.get(name).toFile(name);
                }break;
                case 3:{
                    System.out.println("Ingrese id para automata");
                    name = reader.readLine();
                    Automata tautomata = automataMaker.getDirectAFD();
                    automatas.put(name, tautomata);
                    automatas.get(name).toFile(name);
                }break;
                case 4:{
                    System.out.println("Ingrese la expresion que desea evaluar");
                    name = reader.readLine();
                    System.out.println("Ingrese el id del automata que desea utilizar para la evaluacion");
                    automataName = reader.readLine();
                    if(automatas.get(automataName).getDeterministic())
                        if(afdSimulator.simulate(name, automatas.get(automataName)))
                            System.out.println("Si");
                        else
                            System.out.println("No");
                    else
                        if(afnSimulator.simulate(name, automatas.get(automataName)))
                            System.out.println("Si");
                        else
                            System.out.println("No");
                }break;
                case 5:{
                }break;
                case 6:{
                    exit = true;
                }break;
            }
        }while(!exit);
    }
}
